﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics.Contracts;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
using static Butcher.winform.EnterpriseForms.WhiteNoteList;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Butcher.winform.EnterpriseForms
{
    public partial class WhiteNoteUpdate : Form
    {


        public WhiteNoteUpdate()
        {
            InitializeComponent();


            GETback();

        }
        public string Guid;
        public string kl1;
        public string kl2;
        public string kl3;

        /// <summary>
        /// 白条修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {

            int kl = WhiteNoteList.ids;
            string url = @"http://localhost:5229/api/Batch/WhiteStripWeighingUpdate";
            HttpClient client = new HttpClient();
            GETThey list = new GETThey();
            list.WhiteNoteid = Convert.ToInt32(kl);
            list.Stashid = Convert.ToInt32(Guid);
            list.WhiteNoteTime = Convert.ToDateTime(textBox6.Text);
            list.WhiteNoteNum = Convert.ToInt32(textBox4.Text);
            list.WhiteNoteWeight = Convert.ToInt32(textBox1.Text);
            list.WhiteNoteTare = Convert.ToInt32(textBox2.Text);
            list.NetWeight = Convert.ToInt32(textBox3.Text);
            list.Ductionid = comboBox3.SelectedIndex;
            list.WhiteNoteRemark = textBox5.Text;
            list.SlaughterNumber = textBox7.Text;
            list.WhiteWeightid = Convert.ToInt32(Guid);
            var content = new StringContent(JsonSerializer.Serialize<GETThey>(list), Encoding.UTF8);
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;

            if (result != null)
            {
                MessageBox.Show("修改成功");
            }













        }



        /// <summary>
        /// 赋值
        /// </summary>
        private void GETback()
        {

            int kl = WhiteNoteList.ids;
            string url = @"http://localhost:5229/api/Batch/WhiteStripWeighingBackView";
            HttpClient client = new HttpClient();
            GETid list = new GETid();
            list.Sideproductid = kl;

            var content = new StringContent(JsonSerializer.Serialize<GETid>(list), Encoding.UTF8);
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject josnresult = JObject.Parse(result);
            var results = josnresult["date"];

            textBox7.Text = results["slaughterNumber"].ToString();
            textBox1.Text = results["whiteNoteWeight"].ToString();
            kl1 = results["whiteNoteWeight"].ToString();
            textBox2.Text = results["whiteNoteTare"].ToString();
            kl2 = results["whiteNoteTare"].ToString();
            textBox3.Text = results["netWeight"].ToString();
            kl3 = results["netWeight"].ToString();
            textBox4.Text = results["whiteNoteNum"].ToString();
            textBox5.Text = "";
            comboBox3.Text = results["ductionid"].ToString();
            textBox6.Text = DateTime.Now.ToString();
            Guid = results["stashid"].ToString();
        }



        public class GETid
        {

            public int Sideproductid { get; set; }

        }


        public class GETThey
        {

            public int WhiteWeightid { get; set; }

            /// <summary>
            /// 白条称重主键
            /// </summary>
            public int WhiteNoteid { get; set; }
            /// <summary>
            /// 仓库
            /// </summary>
            public int Stashid { get; set; }
            /// <summary>
            /// 称重时间
            /// </summary>
            public DateTime WhiteNoteTime { get; set; } = DateTime.Now;

            /// <summary>
            /// 称重个数
            /// </summary>
            public int WhiteNoteNum { get; set; }
            /// <summary>
            /// 毛重
            /// </summary>
            public decimal WhiteNoteWeight { get; set; }
            /// <summary>
            /// 皮重
            /// </summary>
            public decimal WhiteNoteTare { get; set; }
            /// <summary>
            /// 净重
            /// </summary>
            public decimal NetWeight { get; set; }
            /// <summary>
            /// 扣减项 id 外键
            /// </summary>
            public int Ductionid { get; set; }
            /// <summary>
            /// 备注详情
            /// </summary>
            public string WhiteNoteRemark { get; set; }
            /// <summary>
            /// 屠宰编号
            /// </summary>
            public string SlaughterNumber { get; set; }




        };

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {


            var gh = Convert.ToInt32(textBox4.Text == "" ? 0 : textBox4.Text);
            if(gh==0)
            {
                textBox1.Text = kl1;
                textBox2.Text = kl2;
                textBox3.Text =kl3;
            }
            else
            {
                textBox1.Text = (Convert.ToInt32(textBox1.Text) * gh).ToString();
                textBox2.Text = (Convert.ToInt32(textBox2.Text) * gh).ToString();
                textBox3.Text = (Convert.ToInt32(textBox3.Text) * gh).ToString();
            }

            




        }
    }
}
